package com.sleet.yuemiao.util;

import com.sleet.yuemiao.enum0.ServiceCodeEnum;
import lombok.*;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author webber
 * @version 1.0
 * @date 2019/11/25 上午11:24
 */
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    /**
     * @ApiModelProperty(value = "返回标记：成功标记=0，失败标记=1")
     */
    private int code;

    @Getter
    @Setter
    /**
     * 返回信息
     */
    private String msg;


    @Getter
    @Setter
    /**
     * 数据
     */
    private T data;



    @Getter
    @Setter
    /**
     * 时间戳
     */
    private Long timestamp;

    public static <T> R<T> ok() {
        return restResult(null, ServiceCodeEnum.SUCCESS, null);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, ServiceCodeEnum.SUCCESS, null);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, ServiceCodeEnum.SUCCESS, msg);
    }

    public static <T> R<T> failed() {
        return restResult(null, ServiceCodeEnum.FAIL, null);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, ServiceCodeEnum.FAIL, msg);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, ServiceCodeEnum.FAIL, null);
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, ServiceCodeEnum.FAIL, msg);
    }

    public static <T> R<T> restResult(T data, ServiceCodeEnum serviceCodeEnum, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(serviceCodeEnum.getCode());
        apiResult.setData(data);
        apiResult.setMsg(StringUtils.isNotBlank(msg) ? msg : serviceCodeEnum.getMsg());
        return apiResult;
    }
}
